/*******************************************************************************
 * Copyright (c) 2010 liw.
 * All rights reserved. 
 * 
 * This file is part of VanBus.
 * 
 *     VanBus is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     VanBus is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with VanBus.  If not, see <http://www.gnu.org/licenses/>.
 * Contributors:
 *     liw - initial API and implementation
 ******************************************************************************/
package org.niclab.vanbus.model.viewadpater;

import java.util.List;

import org.niclab.vanbus.R;
import org.niclab.vanbus.model.BusRoute;
import org.niclab.vanbus.model.BusStop;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class BusStopArrayAdapter extends ArrayAdapter<BusStop>{
	private final static String LOG_TAG="BusStopArrayAdapter";
	
	private Context context;
	private int rowLayout;
	private BusRoute busRoute;
	
	private BusStopArrayAdapter(Context ctx, int layoutId, List<BusStop> stops){
		super(ctx,layoutId,stops);
		this.context = ctx;
		this.rowLayout=layoutId;
		
	}
	
	public BusStopArrayAdapter(Context ctx, int layoutId, BusRoute route){
		this(ctx,layoutId, route.getStops());
		this.busRoute = route;
	}
	
	@Override
	public View getView (int position, View convertView, ViewGroup parent){
		View curView = convertView;
		
		if(curView ==null){
			LayoutInflater li = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			//curView = LayoutInflater.from(this.context).inflate(this.rowLayout,parent,false);
			curView=li.inflate(this.rowLayout,parent,false);
			
		}
		
		BusStop stop = getItem(position);
		
		TextView stopTimesView = (TextView)curView.findViewById(R.id.bus_stop_times_tv);
		String times = stop.getTimesString(busRoute);
		if(times.equals(""))
			times=context.getString(R.string.stop_no_times_found);
		stopTimesView.setText(times);
		
		TextView stopNameView = (TextView) curView.findViewById(R.id.bus_stop_name_tv);
		stopNameView.setText(stop.getStopName());
		
		TextView stopIdView = (TextView) curView.findViewById(R.id.bus_stop_id_tv);
		stopIdView.setText(stop.getStopId());
		
		//set the favorite icon
		ImageView favView = (ImageView)curView.findViewById(R.id.bus_stop_fav);
		if(stop.isFavorite()){
			favView.setImageResource(android.R.drawable.star_big_on);
		}else
			favView.setImageResource(android.R.drawable.star_big_off);
		
		//set the closest indicator view
		ImageView closestView = (ImageView) curView.findViewById(R.id.bus_stop_closest);
		if(stop.isClosest()){
			closestView.setImageResource(android.R.drawable.presence_online);
			
		}else
			closestView.setImageResource(android.R.drawable.presence_invisible);
			
		//set refreshed animation
		if(stop.isRefreshed()){
			Log.v(LOG_TAG,"drawing the refresh animation");
			Animation anim= AnimationUtils.loadAnimation(context,R.anim.refresh_flash);
			/*final TextView refresh = (TextView)curView.findViewById(R.id.bus_stop_refreshed);
			refresh.setText("refreshed");
			Animation anim= AnimationUtils.loadAnimation(context,R.anim.refresh_flash);
				
			anim.setAnimationListener(new AnimationListener(){

				@Override
				public void onAnimationEnd(Animation animation) {
					refresh.setText("");
					Log.d(LOG_TAG,"animation is done");
				}

				@Override
				public void onAnimationRepeat(Animation animation) {
					Log.d(LOG_TAG,"animation repetion ");
				
					
				}

				@Override
				public void onAnimationStart(Animation animation) {
					// TODO Auto-generated method stub
					
				}
				
			});
			refresh.startAnimation(anim);*/
			stopTimesView.startAnimation(anim);
		}		
		//stop.setRefreshed(false);
		return curView;
		
	}

}
